<template>
  <div class="home-page">
    <h1 class="title">Binding</h1>
    <p class="welcome">Hello, Welcome to Bike.</p>
    <div class="input-box">
      <input v-model="mailbox"
        type="email"
        placeholder="Please enter your wallet address">
    </div>
    <div class="input-box"
      style="margin-top: 20px;">
      <input v-model="activationCode"
        type="text"
        placeholder="Please enter the activation code">
    </div>
    <div @click.stop.prevent="firstBinding"
      class="Binding-box">
      <button>Binding</button>
    </div>
  </div>
</template>
<script>
// const REG = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
import { request } from '@/utils/request'
export default {
  props: ['deviceId'],
  data() {
    return {
      mailbox: '',
      activationCode: ''
    }
  },
  created() {},
  methods: {
    firstBinding() {
      let err
      if (!this.mailbox) {
        err = 'Please enter your wallet address'
      } else if (!this.activationCode) {
        err = 'Please enter the activation code'
      } else if (this.activationCode.length != 13) {
        err = 'The activation code length must be 13 bits'
      }

      if (err) {
        plus.nativeUI.toast(err)
        return
      }
      plus.nativeUI.showWaiting()
      request({
        method: 'post',
        url: DOUBANAPI + '/binding',
        data: {
          device_id: this.deviceId,
          email: this.mailbox,
          code: this.activationCode
        }
      })
        .then(res => {
          console.log(res)
          plus.nativeUI.toast('Binding success')
          console.log('device_name', res.data.device_name)
          console.log('wallet_address', res.data.User.email)
          this.$emit('goBegin', {
            deviceName: res.data.device_name,
            walletAddress: res.data.User.email
          })
          plus.nativeUI.closeWaiting()
          this.mailbox = ''
        })
        .catch(err => {
          console.log(err)
          plus.nativeUI.toast(err.response.data.error)
          plus.nativeUI.closeWaiting()
          this.mailbox = ''
          this.activationCode = ''
        })
    }
  }
}
</script>

<style lang="less">
.home-page {
  width: 100%;
  height: 100%;
  padding: 0 40px;
  overflow: hidden;
  background-image: url('../assets/images/login.png');
  background-repeat: no-repeat;
  background-size: cover;
  .title {
    height: 50px;
    line-height: 50px;
    margin-top: 172px;
    font-size: 36px;
    color: #fff;
  }
  .welcome {
    height: 27px;
    line-height: 27px;
    margin-top: 10px;
    color: #bbb;
  }
  .input-box {
    position: relative;
    padding: 10px 0;
    margin-top: 65px;
    input {
      height: 21px;
      width: 100%;
      line-height: 21px;
      font-size: 15px;
      color: #fff;
      &::-webkit-input-placeholder {
        color: #bbbbbb;
      }
      &::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: #bbbbbb;
      }
      &:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 */
        color: #bbbbbb;
      }
      &:-ms-input-placeholder {
        /* Internet Explorer 10-11 */
        color: #bbbbbb;
      }
    }
    &::after {
      content: ' ';
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      height: 1px;
      background-color: #ffffff;
      transform: scaleY(0.5);
    }
  }
  .Binding-box {
    width: 100%;
    height: 44px;
    line-height: 44px;
    margin-top: 50px;
    text-align: center;
    button {
      width: inherit;
      height: inherit;
      font-size: 19px;
      border-radius: 22px;
      color: #fff;
      // background: linear-gradient(to right, #2ad0d3 0%, #3772db 100%);
    }
  }
}
</style>
